package com.masterclient.app.modules.income;

import android.content.Intent;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.contract.UnClearAccountDetailsContract;
import com.masterclient.app.modules.income.contract.UnClearAccountDetailsPresenter;
import com.masterclient.app.modules.income.model.UnClearAccountDetailsBean;
import com.masterclient.app.modules.order.OrderDetailActivity;
import java.util.Objects;
import butterknife.BindView;
import butterknife.OnClick;

@Route(path = AroutePath.UN_CLEAR_ACCOUNT_DETAILS_FRAGMENT)
public class UnClearAccountDetailsFragment extends BaseFragment<UnClearAccountDetailsPresenter> implements UnClearAccountDetailsContract.View {

    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_title_des)
    TextView tvTitleDes;
    @BindView(R.id.view_tip)
    View viewTip;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.view_content)
    LinearLayout viewContent;
    @BindView(R.id.view_account)
    LinearLayout viewAccount;
    @BindView(R.id.view_account_details)
    LinearLayout viewAccountDetails;

    @Autowired(name = "transId")
    String transId;

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_unclear_account_details, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();
        ARouter.getInstance().inject(this);

        mPresenter.getWaitSettleDetail(transId);
    }

    @OnClick({R.id.back})
    void onClick(View view) {
        if (view.getId() == R.id.back) {
            Objects.requireNonNull(getActivity()).finish();
        }
    }

    @Override
    public void showData(UnClearAccountDetailsBean dataBean) {
        if (dataBean != null) {
            if (!TextUtils.isEmpty(dataBean.title)) {
                viewTip.setVisibility(View.VISIBLE);
                tvTip.setText(dataBean.title);
            } else {
                viewTip.setVisibility(View.GONE);
            }
            tvAccount.setText(dataBean.waitSettleAmount);
            tvTitleDes.setText(dataBean.detailTitle);

            initContentListInfo(dataBean);
            initAccountDetailsInfo(dataBean);
        }
    }

    @Override
    public void showInComeData(UnClearAccountDetailsBean dataBean) {

    }

    private void initContentListInfo(UnClearAccountDetailsBean dataBean) {
        if (dataBean.contentList != null && dataBean.contentList.size() > 0) {
            for (int i = 0; i < dataBean.contentList.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_un_clear_account_info, null);
                TextView tvLeftTxt = view.findViewById(R.id.tv_left_txt);
                TextView tvRightTxt = view.findViewById(R.id.tv_right_txt);

                tvLeftTxt.setText(dataBean.contentList.get(i).title);
                if ("0".equals(dataBean.contentList.get(i).jumpType)) {
                    tvRightTxt.setTextColor(Color.parseColor("#ff525866"));
                } else {
                    tvRightTxt.setTextColor(Color.parseColor("#397BE6"));
                    tvRightTxt.setCompoundDrawablesWithIntrinsicBounds(null, null, mContext.getResources().getDrawable(R.mipmap.arrow_right_blue), null);
                    tvRightTxt.setCompoundDrawablePadding(2);
                    int finalI = i;
                    tvRightTxt.setOnClickListener(v -> {
                        if ("1".equals(dataBean.contentList.get(finalI).jumpType)) {
                            //销售订单详情
                            ARouter.getInstance().build(AroutePath.SALE_ORDER_DETAIL_ACTIVITY)
                                    .withString("order_no", dataBean.contentList.get(finalI).jump)
                                    .navigation();
                        } else if ("2".equals(dataBean.contentList.get(finalI).jumpType)) {
                            //采购订单详情
                            Intent intent = new Intent(mContext, OrderDetailActivity.class);
                            intent.putExtra("order_no", dataBean.contentList.get(finalI).jump);
                            intent.putExtra("from_type", "purchase");
                            mContext.startActivity(intent);
                        }
                    });
                }
                tvRightTxt.setText(dataBean.contentList.get(i).content);
                viewContent.addView(view);
            }
            viewContent.setVisibility(View.VISIBLE);
        } else {
            viewContent.setVisibility(View.GONE);
        }
    }

    private void initAccountDetailsInfo(UnClearAccountDetailsBean dataBean) {
        if (dataBean.contentWithdraw != null && dataBean.contentWithdraw.size() > 0) {
            for (int i = 0; i < dataBean.contentWithdraw.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_un_clear_account_details_info, null);
                TextView tvLeftTxt = view.findViewById(R.id.tv_left_txt);
                TextView tvRightTxt = view.findViewById(R.id.tv_right_txt);
                View viewUnderLine = view.findViewById(R.id.view_under_line);

                tvLeftTxt.setText(String.format("%s%s", dataBean.contentWithdraw.get(i).refundTitle, dataBean.contentWithdraw.get(i).refundAmount));
                tvRightTxt.setText(dataBean.contentWithdraw.get(i).refundTime);

                if (i == dataBean.contentWithdraw.size() - 1) {
                    viewUnderLine.setVisibility(View.GONE);
                } else {
                    viewUnderLine.setVisibility(View.VISIBLE);
                }
                viewAccountDetails.addView(view);
            }
            viewAccount.setVisibility(View.VISIBLE);
            viewAccountDetails.setVisibility(View.VISIBLE);
        } else {
            viewAccount.setVisibility(View.GONE);
            viewAccountDetails.setVisibility(View.GONE);
        }
    }

    @Override
    public void showDataFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
